package com.sy.simpleegg.platform.ychsdk.loader

import android.annotation.SuppressLint
import com.sy.simpleegg.platform.ychsdk.api.IConfigApi
import com.sy.simpleegg.platform.ychsdk.api.IGoodsApi
import com.sy.simpleegg.platform.ychsdk.api.IMemberApi
import com.sy.simpleegg.platform.ychsdk.api.IPayApi
import com.sy.simpleegg.platform.ychsdk.bean.HeartResp
import com.sy.simpleegg.platform.ychsdk.bean.YchBuyPackageResp
import com.sy.simpleegg.platform.ychsdk.bean.config.GetPayCoinData
import com.sy.simpleegg.platform.ychsdk.bean.config.GetPayQrCodeData
import com.sy.simpleegg.platform.ychsdk.bean.good.GoodsCoinResponse
import com.sy.simpleegg.platform.ychsdk.bean.good.GoodsResponse
import com.sy.simpleegg.platform.ychsdk.bean.good.RechargeGoodsData
import com.sy.simpleegg.platform.ychsdk.bean.member.GetMemberLoginQrCode
import com.sy.simpleegg.platform.ychsdk.bean.member.MemberInfoResponse
import com.sy.simpleegg.platform.ychsdk.bean.member.QrCodeMemberInfoResponse
import com.sy.simpleegg.platform.ychsdk.bean.member.SaveCoinResult
import com.sy.simpleegg.platform.ychsdk.bean.pay.ChannelWaitOffData
import com.sy.simpleegg.platform.ychsdk.bean.pay.PayResult
import com.sy.simpleegg.platform.ychsdk.constant.Constant
import com.ych.coinsdk.api.ApiWrapper
import com.ych.ytdevicesdk.api.BaseLoader
import com.ych.ytdevicesdk.api.BaseResult
import com.ych.ytdevicesdk.config.Constants
import io.reactivex.Observer

class BusinessLoader: BaseLoader() {

    /**
     * 获取二维码
     * @param transactionId 事务id
     * @return 正扫二维码
     */
    fun getQrCode(transactionId: String?): String {
        TODO("Not yet implemented")
    }

    /**
     * 获取商品列表
     *
     * @param observer 响应
     * @param <T>      设置类型
     */
    @SuppressLint("CheckResult")
    fun getGoodsList(map: Map<String?, Any>, observer: Observer<BaseResult<GoodsResponse?>>?) {
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IGoodsApi::class.java).getGoodsList(
            Constant.TOKEN, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    /**
     * 获得门店兑币商品
     *
     * @param token token
     * @param map   {
     * "IsExchange": true
     * }
     * @return Object
     */
    @SuppressLint("CheckResult")
    fun getPackageList(map: Map<String?, Any?>, observer: Observer<BaseResult<GoodsCoinResponse?>>?) {
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IGoodsApi::class.java).getPackageList(
            Constant.TOKEN, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    /**
     * 获取充值套餐
     */
    @SuppressLint("CheckResult")
    fun getRechargeList(map: Map<String?, Any?>, observer: Observer<BaseResult<RechargeGoodsData?>>?) {
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IGoodsApi::class.java).getMemberRechargeList(
            Constant.TOKEN, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    @SuppressLint("CheckResult")
    fun getConfig(observer: Observer<BaseResult<GetPayCoinData?>>?){
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IConfigApi::class.java).getExchangeConfig(
            Constant.TOKEN
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    /**
     * 获取配置信息（包含纸钞套餐）
     */
    @SuppressLint("CheckResult")
    fun getConfig(map: Map<String?, Any?>, observer: Observer<BaseResult<GetPayCoinData?>>?){
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IConfigApi::class.java).getExchangeConfig(
            Constant.TOKEN, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    /**
     * 上报纸钞订单数据
     */
    @SuppressLint("CheckResult")
    fun pullCashCoinResult(map: Map<String?, Any?>, observer: Observer<BaseResult<Any?>>?){
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IPayApi::class.java).pullCashCoinResult(
            Constant.TOKEN, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    /**
     * 付款码支付信息上报
     */
    @SuppressLint("CheckResult")
    fun placeOrder(map: Map<String?, Any?>, observer: Observer<BaseResult<YchBuyPackageResp?>>?){
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IPayApi::class.java).createOrder(
            Constant.TOKEN, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    /**
     * 获取消费二维码（拼接后使用，扫码进入小程序购币）
     */
    @SuppressLint("CheckResult")
    fun getConsumptionCode(map: Map<String?, Any?>, observer: Observer<BaseResult<GetPayQrCodeData?>>?){
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IConfigApi::class.java).getQrCode(
            Constant.TOKEN, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    /**
     * 设备心跳
     */
    @SuppressLint("CheckResult")
    fun heart(map: Map<String?, Any?>, observer: Observer<BaseResult<HeartResp?>>?){
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IConfigApi::class.java).heart(
            Constant.TOKEN, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    /**
     * 查询支付结果
     *
     * @param orderId  订单ID
     * @param observer 响应
     * @param <T>      设置类型
     */
    @SuppressLint("CheckResult")
    fun queryPayOrder(map: Map<String?, Any?>, observer: Observer<BaseResult<PayResult?>>?) {
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IPayApi::class.java).queryBuyResult(
            Constants.Token, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    /**
     * 登录二维码
     *
     * @param orderId  订单ID
     * @param observer 响应
     * @param <T>      设置类型
     */
    @SuppressLint("CheckResult")
    fun getMemberLoginCode(map: Map<String?, Any?>, observer: Observer<BaseResult<GetMemberLoginQrCode?>>?) {
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IMemberApi::class.java).getMemberLoginCode(
            Constant.TOKEN, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    /**
     * 查询会员扫码登录结果
     *
     * @param orderId  订单ID
     * @param observer 响应
     * @param <T>      设置类型
     */
    @SuppressLint("CheckResult")
    fun queryLgLogin(map: Map<String?, Any?>, observer: Observer<BaseResult<QrCodeMemberInfoResponse?>>?) {
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IMemberApi::class.java).memberLoginQuery(
            Constant.TOKEN, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    /**
     * 查询会员扫码登录结果
     *
     * @param orderId  订单ID
     * @param observer 响应
     * @param <T>      设置类型
     */
    @SuppressLint("CheckResult")
    fun repSaveCoinsData(map: Map<String?, Any?>, observer: Observer<BaseResult<SaveCoinResult?>>?) {
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IMemberApi::class.java).postMemberSaveCoin(
            Constant.TOKEN, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    /**
     * 上报核销数据
     *
     * @param orderId  订单ID
     * @param observer 响应
     * @param <T>      设置类型
     */
    @SuppressLint("CheckResult")
    fun repWriteOffData(map: Map<String?, Any?>, observer: Observer<BaseResult<ChannelWaitOffData?>>?) {
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IPayApi::class.java).writeOffChannelGoodsToCoin(
            Constant.TOKEN, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    /**
     * 上报MQ出币结果
     *
     * @param orderId  订单ID
     * @param observer 响应
     * @param <T>      设置类型
     */
    @SuppressLint("CheckResult")
    fun repMqOutCoinResult(map: Map<String?, Any?>, observer: Observer<BaseResult<Any?>>?) {
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IPayApi::class.java).pullOutCoinResult(
            Constant.TOKEN, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    /**
     * 上报非MQ出币结果
     *
     * @param orderId  订单ID
     * @param observer 响应
     * @param <T>      设置类型
     */
    @SuppressLint("CheckResult")
    fun repOutCoinResult(map: Map<String?, Any?>, observer: Observer<BaseResult<Any?>>?) {
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IPayApi::class.java).pullStartResult(
            Constant.TOKEN, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    /**
     * 登录会员
     *
     * @param type     登录方式
     * @param code     数据内容
     * @param observer 响应
     * @param <T>      设置类型
     */
    @SuppressLint("CheckResult")
    fun loginMember(p0: String?, p1: String?, observer: Observer<BaseResult<MemberInfoResponse?>>?) {
        val map: MutableMap<String?, Any> = mutableMapOf()
        // TODO map数据
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IMemberApi::class.java).memberLogin(
            Constants.Token, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

    @SuppressLint("CheckResult")
    fun memberExtract(p0: Int, observer: Observer<BaseResult<GetMemberLoginQrCode?>>?) {
        val map: MutableMap<String?, Any> = mutableMapOf()
        // TODO map数据
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IMemberApi::class.java).getMemberExtractCoin(
            Constants.Token, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }

//    @SuppressLint("CheckResult")
//    fun platformExtract(p0: String?, p1: Observer<BaseResult<T>>?) {
//        TODO("Not yet implemented")
//    }

    @SuppressLint("CheckResult")
    fun extractComplete(p0: String?, observer: Observer<BaseResult<Any?>>?) {
        val map: MutableMap<String?, Any> = mutableMapOf()
        // TODO map数据
        ApiWrapper.getInstance().setIsDebug(useTestUrl).getService(IPayApi::class.java).pullOutCoinResult(
            Constants.Token, map
        )?.compose(ApiWrapper.getInstance().applySchedulers(observer))
    }
}